package com.lighting.smssystem.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lighting.smssystem.mapper.CompanyMapper;
import com.lighting.smssystem.mapper.FavoriteMapper;
import com.lighting.smssystem.model.Company;
import com.lighting.smssystem.model.UserFavorite;
import com.lighting.smssystem.model.bo.FavoriteList;
import com.lighting.smssystem.model.vo.CompanyFilter;
import com.lighting.smssystem.model.vo.CompanyFilterNew;
import com.lighting.smssystem.model.vo.FavoriteForm;
import com.lighting.smssystem.service.IFavoriteService;
import com.lighting.smssystem.utils.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class FavoriteService implements IFavoriteService {

    @Resource
    private FavoriteMapper favoriteMapper;

    @Resource
    private CompanyMapper companyMapper;


    @Override
    public FavoriteList listUserFavorite(CompanyFilterNew filter) {
        IPage<UserFavorite> userFavoriteIPage = favoriteMapper.selectPage(
                new Page<>(filter.getPageNum(), filter.getPageSize()),
                Wrappers.<UserFavorite>query().eq("favorite_user", filter.getUserId())
        );
        List<Company> companies = new ArrayList<>();
        userFavoriteIPage.getRecords().forEach(fav ->
            companies.add(companyMapper.selectOne(Wrappers.<Company>query().eq("company_id", fav.getFavoriteCompany())))
        );
        Long total = userFavoriteIPage.getTotal();
        return new FavoriteList(total, companies, (long) filter.getPageNum(),
                (long) filter.getPageSize(), (long) Math.ceil(total / filter.getPageSize()));
    }

    @Override
    public int addFavorite(FavoriteForm favoriteForm) {
        List<UserFavorite> userFavorites = favoriteForm.getIds()
                .stream().map(UserFavorite::new).collect(Collectors.toList());
        userFavorites.forEach(fav -> {
            fav.setFavoriteId(SnowflakeIdWorker.next());
            fav.setFavoriteUser(favoriteForm.getId());
            List<UserFavorite> favorite = favoriteMapper.selectList(Wrappers.<UserFavorite>query()
                    .eq("favorite_company", fav.getFavoriteCompany())
                    .eq("favorite_user", fav.getFavoriteUser()));
            if (favorite.size() <= 0) {
                favoriteMapper.insert(fav);
            }
        });
        return userFavorites.size();
    }

    @Override
    public int delFavorite(Long userId, String ids) {
        List<Long> favorits = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        favorits.forEach(fav -> favoriteMapper.delete(Wrappers.<UserFavorite>query()
                .eq("favorite_company", fav)
                .eq("favorite_user", userId)));
        return favorits.size();
    }
}
